<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">










<html>
  <head>
    <title>log4php - Apache log4php usage example</title>
    <style type="text/css" media="all">
      @import url("./css/maven-base.css");
      @import url("./css/maven-theme.css");
      @import url("./css/site.css");
    </style>
    <link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
      </head>
  <body class="composite">
    <div id="banner">
                  <a href="http://logging.apache.org/" id="bannerLeft">
    
                                            <img src="images/ls-logo.jpg" alt="" />
    
            </a>
                    <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="breadcrumbs">
          
  

  
    
  
  
            <div class="xleft">
        Last Published: 07/23/2007
                          |   
          <a href="http://www.apache.org/">Apache</a>
              &gt;
      
          <a href="http://incubator.apache.org/">Incubator</a>
              &gt;
      
          log4php
                        </div>
            <div class="xright">      
  

  
    
  
  
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="leftColumn">
      <div id="navcolumn">
           
  

  
    
  
  
                   <h5>Get log4php</h5>
        <ul>
              
    <li class="none">
              <a href="download.html">Download</a>
        </li>
              
    <li class="none">
              <a href="changes-report.html">Changes</a>
        </li>
              
    <li class="none">
              <a href="license.html">License</a>
        </li>
          </ul>
          <h5>About log4php</h5>
        <ul>
              
    <li class="none">
              <a href="index.html">What is it?</a>
        </li>
              
    <li class="none">
              <a href="qsg.html">Quick start</a>
        </li>
              
    <li class="none">
              <strong>Usage</strong>
        </li>
              
    <li class="none">
              <a href="roadmap.html">Roadmap</a>
        </li>
              
    <li class="none">
              <a href="apidocs/index.html">PHPDoc</a>
        </li>
          </ul>
          <h5>Community</h5>
        <ul>
              
    <li class="none">
              <a href="mail-lists.html">Mailing Lists</a>
        </li>
              
    <li class="none">
              <a href="issue-tracking.html">Issue Tracking</a>
        </li>
          </ul>
          <h5>Development</h5>
        <ul>
              
    <li class="none">
              <a href="source-repository.html">Repository</a>
        </li>
          </ul>
          <h5>Apache</h5>
        <ul>
              
    <li class="none">
              <a href="http://www.apache.org">Home</a>
        </li>
              
    <li class="none">
              <a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a>
        </li>
              
    <li class="none">
              <a href="http://www.apache.org/foundation/thanks.html">Thanks</a>
        </li>
              
    <li class="none">
              <a href="http://www.apachecon.com">Conferences</a>
        </li>
          </ul>
                                       <a href="http://maven.apache.org/" title="Built by Maven" id="poweredBy">
            <img alt="Built by Maven" src="./images/logos/maven-feather.png"></img>
          </a>
                       
  

  
    
  
  
        </div>
    </div>
    <div id="bodyColumn">
      <div id="contentBox">
        <div class="section"><h2>Apache Log4php Usage Example&quot;&gt;</h2><div class="source"><pre>&lt;?php 
/**
 * This is a log4php usage example file
 *
 */

/**
 * Set LOG4PHP_DIR to Your log4php root dir or it will
 * be set automatically at the first require_once().
 * Here 'log4php' is an 'include_path' subdir.
 */
define('LOG4PHP_DIR', 'log4php');

/*
    1.  If you want to use a custom Configurator,
        set the LOG4PHP_CONFIGURATOR_CLASS constants to Your Configurator class file.
        The class name must have the same base name of the classfile.

         Ex: define('LOG4PHP_CONFIGURATOR_CLASS', '/my/path/LoggerMyConfigurator');

          Log4php will try to include '/my/path/LoggerMyConfigurator.php'
          and instantiate a 'LoggerMyConfigurator' class.

    2.  If you want to use a configuration file that's not the default,
        set the LOG4PHP_CONFIGURATION constants to Your configuration filename.

        Ex: define('LOG4PHP_CONFIGURATION', '/my/path/my_config.conf');

        Note that if config extension is NOT .xml and LOG4PHP_CONFIGURATOR_CLASS
        is not defined, the LoggerPropertyConfigurator will be used.

    3.  If you want to bypass the initial configuration procedure, set the 
        'LOG4PHP_DEFAULT_INIT_OVERRIDE' to true.

         Ex: define('LOG4PHP_DEFAULT_INIT_OVERRIDE', true);
*/

require_once(LOG4PHP_DIR. '/LoggerManager.php');
/* 
    Or You can use:

    require_once('/my/log4php/path/LoggerManager.php');

    and LOG4PHP_DIR will be automatically set to '/my/log4php/path'.
*/

class Test {

    var $logger;

    function Test()
    {
        $this-&gt;logger =&amp; LoggerManager::getLogger('Test');
    }

    function testLog()
    {
        $this-&gt;logger-&gt;debug('this is a DEBUG log generated by Test::testLog() class');
        $this-&gt;logger-&gt;info('this is an INFO log generated by Test::testLog() class');
        $this-&gt;logger-&gt;warn('this is a WARN log generated by Test::testLog() class');
        $this-&gt;logger-&gt;error('this is an ERROR log generated by Test::testLog() class');
        $this-&gt;logger-&gt;fatal('this is a FATAL log generated by Test::testLog() class');
     }
}

class TestTest extends Test {
    var $logger;

    function TestTest()
    {
        $this-&gt;Test();
        $this-&gt;logger =&amp; LoggerManager::getLogger('Test.Test');
    }

    function testLog()
    {
        LoggerNDC::push('NDC generated by TestTest::testLog()');

        $this-&gt;logger-&gt;debug('this is a DEBUG log generated by TestTest::testLog() class');
        $this-&gt;logger-&gt;info('this is an INFO log generated by TestTest::testLog() class');
        $this-&gt;logger-&gt;warn('this is a WARN log generated by TestTest::testLog() class');
        $this&gt;-&gt;logger-&gt;error('this is an ERROR log generated by TestTest::testLog() class');
        $this-&gt;logger-&gt;fatal('this is a FATAL log generated by TestTest::testLog() class');

        LoggerNDC::pop();
    }
} 

function Bar()
{
    $logger =&amp; LoggerManager::getLogger('bar');

    /* 
    note that the message here is an array
    */ 
    
    $logger-&gt;debug(array('one', 'two', 'tree'));
    $logger-&gt;info('this is an INFO log generated by Bar() function');
    $logger-&gt;warn('this is a WARN log generated by Bar() function');
    $logger-&gt;error('this is an ERROR log generated by Bar() function');
    $logger-&gt;fatal('this is a FATAL log generated by Bar() function');
}

$logger =&amp; LoggerManager::getLogger('main');
$logger-&gt;debug('this is a DEBUG log generated by main() function');
$logger-&gt;info('this is an INFO log generated by main() function');
$logger-&gt;warn('this is a WARN log generated by main() function');
$logger-&gt;error('this is an ERROR log generated by main() function');
$logger-&gt;fatal('this is a FATAL log generated by main() function')

$test = new Test();
$test-&gt;testLog();

$testTest = new TestTest();
$testTest-&gt;testLog();

Bar();
?&gt;</pre></div></div>
      </div>
    </div>
    <div class="clear">
      <hr/>
    </div>
    <div id="footer">
      <div class="xright">&#169;  
          2007
    
          Apache Software Foundation
          
  

  
    
  
  
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
  </body>
</html>
